package com.bcu.wish.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bcu.wish.config.GlobalConfig;
import com.bcu.wish.entity.*;
import com.bcu.wish.mapper.UserMapper;
import com.bcu.wish.mapper.UserinfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.bcu.wish.mapper.BeginwishMapper;
import com.bcu.wish.service.BeginwishService;

import java.util.*;
import java.util.function.IntFunction;

@Service
public class BeginwishServiceImpl implements BeginwishService {

    @Resource
    private BeginwishMapper beginwishMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private GlobalConfig globalConfig;

    @Override
    public Message deleteByPrimaryKey(Integer beginwishId) {
        return beginwishMapper.deleteByPrimaryKey(beginwishId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Beginwish record) {
        return beginwishMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Beginwish record) {
        return beginwishMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Beginwish> selectByPrimaryKey(Integer beginwishId) {
        return Message.success(null).add(beginwishMapper.selectByPrimaryKey(beginwishId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Beginwish record) {
        return beginwishMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Beginwish record) {
        return beginwishMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<List<Beginwish>> selectAll() {
        return Message.success(null).add(beginwishMapper.selectAll());
    }

    @Override
    public Message getExcel() {
        List<Beginwish> beginwishList = beginwishMapper.selectAll();
        ArrayList rows = new ArrayList();
        for (int i = 0; i < beginwishList.size(); i++) {
            Beginwish beginwish = beginwishList.get(i);
            Map<String, Object> row = new LinkedHashMap<>();
            row.put("学号", beginwish.getUserId());
            Userinfo userinfo = userinfoMapper.selectByPrimaryKey(beginwish.getUserId());
            row.put("学生姓名", userinfo.getUserinfoUserName());
            row.put("高考成绩",beginwish.getUserGrade());
            row.put("第一志愿", beginwish.getBeginwishFirst());
            row.put("第二志愿", beginwish.getBeginwishSecond());
            row.put("第三志愿", beginwish.getBeginwishThird());
            row.put("第四志愿", beginwish.getBeginwishFourth());
            row.put("第五志愿", beginwish.getBeginwishFifth());
            rows.add(row);
        }
        ArrayList<Map<String, Object>> allrows = CollUtil.newArrayList(rows);
        // 通过工具类创建writer
        System.out.println(globalConfig.filePath);
        ExcelWriter writer = ExcelUtil.getWriter(globalConfig.filePath + "beginwish.xlsx");
        // 合并单元格后的标题行，使用默认标题样式
        //writer.merge(rows.size() - 1, "志愿填报");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(allrows, true);
        // 关闭writer，释放内存
        writer.close();
        String returnUrl = globalConfig.hostUrl + "resource/" + "beginwish.xlsx";

        return Message.success(null).add(returnUrl);
    }

    @Override
    public Message<Beginwish> selectByUserId(String userId) {
        return Message.success(null).add(beginwishMapper.selectByUserId(userId));
    }

    @Override
    public Message ceshi() {

        HashMap hashMap = new HashMap();
        List<Beginwish> beginwishList = beginwishMapper.selectAll();
        int i=0;
        int a=0;
        int b=0;
        int c=0;
        int d=0;
        int e=0;
        for (Beginwish beginwish:beginwishList){
            i++;
            if (beginwish.getBeginwishFirst().equals("英语")){
                a++;
            }
            else if (beginwish.getBeginwishFirst().equals("翻译")){
                b++;
            }
            else if (beginwish.getBeginwishFirst().equals("德语")){
                c++;
            }
            else if (beginwish.getBeginwishFirst().equals("西语")){
                d++;
            }
            else if (beginwish.getBeginwishFirst().equals("法语")){
                e++;
            }
        }
        hashMap.put("目前填写专业预填报的总人数为",i);
        hashMap.put("第一专业为英语的人数为",a);
        hashMap.put("第一专业为翻译的人数为",b);
        hashMap.put("第一专业为德语的人数为",c);
        hashMap.put("第一专业为西语的人数为",d);
        hashMap.put("第一专业为法语的人数为",e);

        return Message.success(null).add(hashMap);
    }

    @Override
    public Message ceshi2() {
        List<User> users = new LinkedList<>();
       List<User> userList = userMapper.selectAll();
       for (User user:userList){
           Beginwish beginwish = beginwishMapper.selectByUserId(user.getUserId());
           if (beginwish==null){
                users.add(user);
           }
       }
       return Message.success(null).add(users);
    }
}

